﻿using System;
using System.IO;
using System.Xml.Serialization;
using MobySharp.Results;

namespace MobySharp.Actions
{
    public class GetUser : BaseAction, IAction
    {
        private readonly string _username;

        public GetUser(string username)
        {
            _username = username;
            RequestType = HTTPRequestType.Get;
            Action = APICall.getUser;
        }

        [PostDataInstruction("u", true, 32)]
        public string Username
        {
            get { return _username; }
        }

        #region IAction Members

        public IResult ParseResult(string result)
        {
            var r = new UserResult();

            try
            {
                // Wacky check if we recieved some XML (you never know).
                if (result.Contains("<?xml"))
                {
                    var serializer = new XmlSerializer(r.GetType());
                    r = (UserResult) serializer.Deserialize(new StringReader(result));

                    return r;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            return r;
        }

        #endregion
    }
}